// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.+
// @description  try to take over the world!
// @author       You
// @match        http://image.baidu.com/search/index?*
// @require      https://code.jquery.com/jquery-3.3.1.min.js
// @grant        none
// ==/UserScript==

(function () {
	'use strict';

	//提交按钮添加到页面
	$("#imgfilter-btn").after("<button class='tjButton' type='button' value='' name='test' style='width:80px;height:30px;background-color:#ff6e2e;'>提交</button>");
	var i = 0;

	//img_checkbox
	$(".imgitem").each(function () {
		//console.log(1);
		i++;
		$(this).append("<input class='checkbox' type='checkbox' id='tests" + i + "' name='test" + i + "' value='' checked='' style='width:100px;height:60px;position:absolute;top:0;' />");
	});

	// js监测checkbox是否被选中
	$(".checkbox").prop("checked", false);

	//获取图片真实地址
	var img_urls = $(".imgbox a");

	for (var i = 0; i < img_urls.length; i++) {
		var url_href = img_urls[i].href;
		var url = url_href.split("objurl=")[1].split("&")[0];
		// console.log(decodeURIComponent(url));

		var decodeUrl = decodeURIComponent(url);
		//var checkbox_val = document.querySelectorAll(".checkbox").value = decodeUrl;
		var name = "#test" + (i + 1);
		$(name).val(decodeUrl);
		//console.log($(name));
	}

	//将选中img真实URL一键获取
	$(".tjButton").click(function () {
		console.log($(".checkbox"));
		var checkBoxs = $(".checkbox");
		for (var j = 0; j < checkBoxs.length; j++) {
			// console.log(checkBoxs[j].value);
		}

		if ($(".checkbox").is(':checked')) {
			console.log($("#tests"));
		}

	});

	//监听鼠标滚动，动态添加checkbox
	var scrollFunc = function (e) {
		e = e || window.event;

		if (e.wheelDelta) { //IE/Opera/Chrome
			// console.log(e.wheelDelta);
			$(".imgitem").each(function () {
				console.log(1);
				var i = 0;
				i++;
				$(this).append("<input class='checkbox' type='checkbox' name='test' value='' checked='' style='width:100px;height:60px;position:absolute;top:0;' />");
			});

			// js监测checkbox是否被选中
			$(".checkbox").prop("checked", false);

		} else if (e.detail) { //Firefox
			console.log(e.detail);
		}
	};

	if (document.addEventListener) {
		document.addEventListener('DOMMouseScroll', scrollFunc, false);
	}
	window.onmousewheel = document.onmousewheel = scrollFunc; //IE/Opera/Chrome

})();






// var obj = document.querySelectorAll(".checkbox").value;
// var check_val = [];
// check_val = checkbox_val;
// var k;
// for (k in obj) {
// 	if (obj[k].checked)
// 		check_val.push(obj[k].value);
// }
// console.log(check_val);
/*
		$.ajax({
		    url: "http://192.168.1.82:8000/api/ims/v1.0/images/",
		    async:false,
		    type:"POST",
		    dataType:"jsonp",
		    success: function (result) {
		        console.log(result);
		    }
		});
*/